package fr.flogo.destinee;

import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import fr.flogo.destinee.cameras.ICamera;
import fr.flogo.destinee.cameras.IsometricSimpleCamera;
import fr.flogo.destinee.cameras.KaylFreefly;
import fr.flogo.destinee.cameras.VerySimple2DCamera;
import fr.flogo.destinee.cameras.VerySimpleCamera;
import fr.flogo.destinee.objects.IGLObject;
import fr.flogo.destinee.objects.statics.Cube;
import fr.flogo.destinee.objects.textures.TexturesUtils;
import fr.flogo.destinee.pads.FullScreen4dPad;
import fr.flogo.destinee.positions.Vector3D;
import fr.flogo.destinee.renderers.FirstPersonRenderer;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class DestineeActivity extends Activity {
	private Renderer renderer;
	private GLSurfaceView glSurfaceView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// requesting to turn the title OFF
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// making it full screen
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		this.glSurfaceView = new GLSurfaceView(this);
		this.renderer = new FirstPesonRendererSceneTest(this, this.glSurfaceView);
		glSurfaceView.setRenderer(this.renderer);
		setContentView(glSurfaceView);
	}

	public boolean onTouchEvent(final MotionEvent event) {
		
		if (FullScreen4dPad.getInstance() != null) {
			FullScreen4dPad.getInstance().onTouch(event);
			return true;
		} else {
			System.out.println("Pad is not ready");
			return false;
		}
	}

	// TODO faire de vrai classes
	public static class TextureDestineeSceneTest extends TexturesUtils {
		private IntBuffer texturesBuffer[];
		final static int nTextures = 4;

		public enum TexturesNames {
			Mur, Caisse, Sol, Voute
		}

		public TextureDestineeSceneTest(GL10 gl, Context context)
				throws Exception {
			super(gl, context);
			this.texturesBuffer = new IntBuffer[nTextures];
			this.texturesBuffer[0] = this.loadNewTexture(gl,
					R.drawable.murchateau);
			this.texturesBuffer[1] = this.loadNewTexture(gl, R.drawable.crate);
			this.texturesBuffer[2] = this.loadNewTexture(gl,
					R.drawable.solchateau);
			this.texturesBuffer[3] = this.loadNewTexture(gl, R.drawable.voute);
		}

		@Override
		public IntBuffer getTexture(int id) {
			if (id < this.texturesBuffer.length)
				return this.texturesBuffer[id];
			return null;
		}

	}

	private class FirstPesonRendererSceneTest extends FirstPersonRenderer {
		private TexturesUtils texturesUtils = null;
		private Context context = null;
		private IGLObject objects[];
		private float z = -5.0f;
		private float translation = -6f;
		private ICamera camera;
		private FullScreen4dPad pad;
		private World world = null;

		public FirstPesonRendererSceneTest(Context context, GLSurfaceView surface) {
			this.context = context;
			objects = new IGLObject[7];
//			this.camera = new VerySimpleCamera();
//			this.camera = new KaylFreefly(new Vector3D(0d, 0d, 0d));
//			this.camera = new IsometricSimpleCamera();
			this.camera = new VerySimple2DCamera();
			this.pad = new FullScreen4dPad(surface, camera);
			
		}

		@Override
		public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
			super.onSurfaceCreated(gl, eglConfig);

			try {
				this.texturesUtils = new TextureDestineeSceneTest(gl, context);
				this.world = new World(this.context, "carre", this.texturesUtils);
				// Murs :
				objects[0] = new Cube(this.texturesUtils.getTexture(
						TextureDestineeSceneTest.TexturesNames.Mur.ordinal())
						.get(0));
				objects[0].translate(0f, 0, 0);
//				objects[1] = new Cube(this.texturesUtils.getTexture(
//						TextureDestineeSceneTest.TexturesNames.Mur.ordinal())
//						.get(0));
//				objects[1].translate(2f, 0, 2.0f);
//				objects[2] = new Cube(this.texturesUtils.getTexture(
//						TextureDestineeSceneTest.TexturesNames.Mur.ordinal())
//						.get(0));
//				objects[2].translate(-2f, 0, 0);
//				objects[3] = new Cube(this.texturesUtils.getTexture(
//						TextureDestineeSceneTest.TexturesNames.Mur.ordinal())
//						.get(0));
//				objects[3].translate(-2f, 0, 2.0f);
//
//				// sols
//				objects[4] = new Cube(this.texturesUtils.getTexture(
//						TextureDestineeSceneTest.TexturesNames.Sol.ordinal())
//						.get(0));
//				objects[4].translate(0f, -2, 0.0f);
//				objects[5] = new Cube(this.texturesUtils.getTexture(
//						TextureDestineeSceneTest.TexturesNames.Sol.ordinal())
//						.get(0));
//				objects[5].translate(0f, -2, 2.0f);
//
//				// voute
//				objects[6] = new Cube(this.texturesUtils.getTexture(
//						TextureDestineeSceneTest.TexturesNames.Voute.ordinal())
//						.get(0));
//				objects[6].translate(0f, 0f, -2f);
//				objects[6].rotation(90, 0f, 0f, 1f);

				float[] pos = this.world.getStartPosition();
//				this.camera.translate(gl, pos[0], pos[1], pos[2]);
//				this.camera.translate(gl, pos[0], pos[2], pos[1]);
//				this.camera.rotation(gl, 90.0f, 1.0f, 0f, 0f);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO afficher message d'erreur
			}
		}

		@Override
		public void onDrawFrame(GL10 gl) {
			super.onDrawFrame(gl);
			// gl.glTranslatef(0.0f, 0.0f, this.translation ); // Move z units
			// into the screen
			// gl.glRotatef(rotation, 1.0f, 1.0f, 0f);
			this.camera.drawMovement(gl);
//			gl.glScalef(0.6f, 0.6f, 0.6f);
			// TODO scale dans la camera
			// Scale the Cube to 80 percent,
			// otherwise it would be too large
			// for the screen

			this.world.draw(gl);
			
//			objects[0].draw(gl);
			
//			int i = 0;
//			while (i < this.objects.length) {
//				objects[i].draw(gl);
//				i++;
//			}

//			this.translation += 0.01f;
			// this.rotation += 0.3;
		}

		private float rotation = 0.0f;
	}

}